<template>
  <div class="container">
    <el-card class="box-card">
      <el-row style="height: 40px" class="father">
        <el-col :span="20" justify="start">
          <span class="subjectName">学科名称</span>
          <el-input
            style="width: 150px"
            v-model="input"
            size="small"
          ></el-input>
          <el-button size="small" @click="eliminate">清除</el-button>
          <el-button type="primary" size="small" @click="search"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end">
            <!-- 两个内容 -->
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="addSubjects"
              >新增学科</el-button
            >
          </el-row>
        </el-col>
      </el-row>

      <div class="tips">
        <i class="el-icon-info" style="width: 20px"></i>
        <span>数据一共{{ total }}条</span>
      </div>

      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="学科名称" prop="subjectName"> </el-table-column>
        <el-table-column label="创建者" prop="username" width="100">
        </el-table-column>
        <el-table-column label="创建日期" width="170">
          <template #default="{ row }">
            {{ fromatTime(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示" prop="isFrontDisplay">
          <template #default="{ row }">
            {{ formatisFrontDisplay(row.isFrontDisplay) }}
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory">
        </el-table-column>
        <el-table-column label="标签" prop="tags"> </el-table-column>
        <el-table-column label="题目数量" prop="totals"> </el-table-column>
        <el-table-column label="操作" width="250px">
          <template #default="{ row }">
            <el-button
              type="text"
              size="small"
              @click="
                $router.push({
                  path: '/subjects/directorys',
                  query: { id: row.id, name: row.subjectName },
                })
              "
              >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="
                $router.push({
                  path: '/subjects/tags',
                  query: { id: row.id, name: row.subjectName },
                })
              "
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="update(row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="Delete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.params.page"
          :page-size="this.params.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增弹框 -->
    <subjects-add
      :dialog-visible="dialogVisible"
      @close-dialog="closeDialog"
      @update-list="getList"
      ref="addlist"
      @change-page="params.page = 1"
    ></subjects-add>
  </div>
</template>

<script>
import SubjectsAdd from '../components/subjects-add.vue'
import { list, remove } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {
  components: {
    SubjectsAdd
  },
  data () {
    return {
      tableData: [],
      params: {
        page: 1,
        pagesize: 10,
        keyword: '',
        subjectID: '',
        subjectName: ''
      },
      total: 0,
      input: '',
      dialogVisible: false
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList () {
      this.get_list()
      // console.log(res.data.counts)
    },
    // 格式化日期
    fromatTime (value) {
      return dayjs(value).format('YYYY-MM-DD hh:mm:ss')
    },
    // 前台是否显示
    formatisFrontDisplay (value) {
      const MAP = {
        0: '否',
        1: '是',
        100: '否'
      }
      return MAP[value]
    },
    // 分页
    handleCurrentChange (page) {
      this.params.page = page
      this.getList()
      // console.log(page)
    },
    // 控制每页多少条函数
    handleSizeChange (pagesize) {
      this.params.pagesize = pagesize
      this.getList()
    },
    // 清除
    eliminate () {
      this.input = ''
      this.params.subjectName = ''
      this.getList()
    },
    // 搜索
    async search () {
      this.params.subjectName = this.input
      this.get_list()
    },
    // 包装公共函数
    async get_list () {
      const res = await list(this.params)
      console.log(res.data)
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    // 打开新增弹框
    addSubjects () {
      this.dialogVisible = true
    },
    // 关闭新增弹框
    closeDialog () {
      this.dialogVisible = false
    },
    // 修改学科名称
    update (row) {
      this.dialogVisible = true
      this.$refs.addlist.updatelist(row)
    },
    // 删除学科
    Delete (row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    // 新增业务
    // addlist () { }
  }
}
</script>

<style scoped lang='css'>
.subjectName {
  font-size: 14px;
  font-weight: 700;
  color: rgb(91, 90, 90);
  margin-left: 20px;
}
.el-input {
  margin-right: 10px;
  margin-left: 10px;
}
.el-button {
  margin-right: 5px;
}
.tips {
  width: 100%;
  height: 40px;
  color: rgb(145, 147, 153);
  font-size: 13px;
  background-color: rgb(244, 244, 245);
  padding-top: 13px;
  padding-left: 15px;
  border-radius: 5px;
  margin-bottom: 20px;
}
.block {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
.box-card {
  margin: 0.5%;
}
</style>
